<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '@/components/Charts/mixins/resize'

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    lineColor:{
      type:String,
      default:'rgb(137,189,27)'
    },
    barColor:{
      type:String,
      default:'rgb(137,189,27)'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ names, messures, xValues, yValues } = {}){
      let yAx = []
      let sers = []
      for (let idx in messures){
        yAx.push({
          type: 'value',
          name:names[idx],
          axisLabel: {
            formatter: `{value} ${messures[idx]}`
          }
        })
        let comOpt = {
          name: names[idx],
          type: 'bar',
          barWidth: '60%',
          yAxisIndex:idx,
          tooltip: {
            valueFormatter: function (value) {
              return value + messures[idx];
            }
          },
          itemStyle: {
            normal: {
              color: this.barColor
            }
          },
          data: yValues[idx],
          animationDuration
        }
        if (idx>0){
          comOpt.type='line'
          comOpt.itemStyle.normal.color=this.lineColor
          delete comOpt['barWidth']
        }
        sers.push(comOpt)
      }
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        dataZoom: [
          {
              type: 'inside',
              xAxisIndex: 0,
          },
          {
              type: 'inside',
              zoomOnMouseWheel:true,
          }
        ],
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          icon: 'rect',
          itemWidth: 14,
          itemHeight: 5,
          itemGap: 13,
          data: names,
          textStyle: {
            fontSize: 12,
            color: '#57617B'
          }
        },
        xAxis: [{
          type: 'category',
          axisLabel: {
            rotate:30,
            showMinLabel:true
          },
          data: xValues,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: yAx,
        series: sers
      })
    }
  }
}
</script>
